package com.youlai.boot.system.converter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.model.Option;
import com.youlai.boot.system.model.entity.User;
import com.youlai.boot.system.model.entity.UserPoints;
import com.youlai.boot.system.model.form.WorkForm;
import com.youlai.boot.system.model.vo.UserInfoVO;
import com.youlai.boot.system.model.vo.UserPageVO;
import com.youlai.boot.system.model.vo.UserProfileVO;
import com.youlai.boot.system.mapper.UserPointsMapper;
import com.youlai.boot.system.model.bo.UserBO;
import com.youlai.boot.system.model.form.UserForm;
import com.youlai.boot.system.model.dto.UserImportDTO;
import com.youlai.boot.system.model.form.UserProfileForm;
import com.youlai.boot.system.model.vo.WorkerPageVO;

import cn.hutool.core.util.ObjectUtil;

import org.mapstruct.Context;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户对象转换器
 *
 * @author Ray.Hao
 * @since 2022/6/8
 */
@Mapper(componentModel = "spring")
public interface UserConverter {

        @Mapping(target = "balance", source = "balance")
        @Mapping(target = "orgId", source = "orgId")
        UserPageVO toPageVo(UserBO bo);

        Page<UserPageVO> toPageVo(Page<UserBO> bo);

        Page<WorkerPageVO> toWorkerPageVo(Page<UserBO> bo);

        UserForm toForm(User entity);

        @InheritInverseConfiguration(name = "toForm")
        User toEntity(UserForm entity);

        User toEntity(WorkForm entity);

        @Mappings({
                        @Mapping(target = "userId", source = "id"),
                        @Mapping(target = "orgId", source = "orgId")
        })
        UserInfoVO toUserInfoVo(User entity);

        User toEntity(UserImportDTO vo);

        UserInfoVO toUserInfoVO(UserBO bo);

        @Mappings({
                        @Mapping(target = "balance", source = "bo.balance"),
                        @Mapping(target = "points", expression = "java(getPoints(bo,userPointsMapper))")
        })
        UserProfileVO toProfileVO(UserBO bo, @Context UserPointsMapper userPointsMapper);

        User toEntity(UserProfileForm formData);

        @Mappings({
                        @Mapping(target = "label", source = "nickname"),
                        @Mapping(target = "value", source = "id")
        })
        Option<String> toOption(User entity);

        List<Option<String>> toOptions(List<User> list);

        default BigDecimal getPoints(UserBO bo, UserPointsMapper userPointsMapper) {
                Long userId = bo.getId();
                UserPoints userPoints = userPointsMapper
                                .selectOne(new QueryWrapper<UserPoints>().eq("user_id", userId));
                if (ObjectUtil.isNull(userPoints)) {
                        return BigDecimal.ZERO;
                } else {
                        return userPoints.getRemainPoints();
                }
        }
}
